// use dotenv::dotenv;
use anyhow::{Context, Error};
use std::fs;

#[derive(Debug, serde::Deserialize, Clone)]
pub struct AppConfig {
    pub database_url: String,

    pub minio_endpoint: String,
    pub minio_access_key: String,
    pub minio_secret_key: String,
    pub minio_bucket: String,

    pub jwt_secret: String,

    pub admin_usrername: String,
    pub admin_password: String,

    pub server_port: u16,
}

// TODO: envy

impl AppConfig {
    pub async fn load() -> Result<Self, Error> {
        let config:AppConfig;
        #[cfg(debug_assertions)]
        {
            let config_str = fs::read_to_string("./config_dev.toml").context("Failed to read config file")?;
            config = toml::from_str(&config_str)?;
        }
        
        #[cfg(not(debug_assertions))] {
            let config_str = fs::read_to_string("./config.toml").context("Failed to read config file")?;
            config = toml::from_str(&config_str)?;
        }
         
        Ok(config)
    }
}